/**
*      author: Y_Yao
*      created: 2024-11-26 16:21:22
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 7;
int t[N],v[N];
int T,M;

void solve()
{
    cin >> T >> M;
    vector<int> dp(T + 1,0);
    for(int i = 1;i <= M;i++)
    {
        cin >> t[i] >> v[i];
    }

    for(int i = 1;i <= M;i++)
    {
        for(int j = T;j >= 1;j--)
        {
            if(j - t[i] >= 0)
            {
                dp[j] = max(dp[j],dp[j - t[i]] + v[i]);
            }
        }
    }

    cout << dp[T] << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}